package com.zhitan.ems.mini.api;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线程执行的上下文内容
 * 
 * @author zhitan
 */
public class ApiThreadContext {
    /**
     * 线程上下文变量的持有者
     */
    private final static ThreadLocal<Map<String, Object>> CTX_HOLDER_API = new ThreadLocal<Map<String, Object>>();

    static {
    	init();
    }

    /**
     * 添加内容到线程上下文中
     * 
     * @param key
     * @param value
     */
    public final static void putContext(String key, Object value) {
        Map<String, Object> ctx = CTX_HOLDER_API.get();
        if (ctx == null) {
        	return;
        }
        ctx.put(key, value);
    }

    /**
     * 从线程上下文中获取内容
     * 
     * @param key
     */
    @SuppressWarnings("unchecked")
    public final static <T extends Object> T getContext(String key) {
        Map<String, Object> ctx = CTX_HOLDER_API.get();
        if (ctx == null) {
            return null;
        }
        return (T) ctx.get(key);
    }
    
    /**
     * 获取线程上下文
     * 
     * @param key
     */
    public final static Map<String, Object> getContext() {
        Map<String, Object> ctx = CTX_HOLDER_API.get();
        if (ctx == null) {
            return null;
        }
        return ctx;
    }

    /**
     * 删除上下文中的key
     * 
     * @param key
     */
    public final static void remove(String key) {
        Map<String, Object> ctx = CTX_HOLDER_API.get();
        if (ctx != null) {
            ctx.remove(key);
        }
    }

    /**
     * 上下文中是否包含此key
     * 
     * @param key
     * @return
     */
    public final static boolean contains(String key) {
        Map<String, Object> ctx = CTX_HOLDER_API.get();
        if (ctx != null) {
            return ctx.containsKey(key);
        }
        return false;
    }

    /**
     * 清空线程上下文
     */
    public final static void clean() {
        CTX_HOLDER_API.set(null);
    }

    /**
     * 初始化线程上下文
     */
    public final static void init() {
    	 Map<String, Object> ctx = CTX_HOLDER_API.get();
         if (ctx == null) {
             CTX_HOLDER_API.set(new HashMap<String, Object>());
    	}
    }
    
    /**
     * 获取用来做分库分表的key
     * @return
     */
    @SuppressWarnings("unchecked")
	public final static <K extends Serializable> K getShardKey(){
    	return (K)getContext(SHARD_KEY);
    }
    
    /**
     * 设置做分表分库的切分的key
     * @param shardKey
     */
    public final static <K extends Serializable> void putShardKey(K shardKey){
    	putContext(SHARD_KEY, shardKey);
    }
    
    /**
     * 获取Session中的用户信息
     * @return
     */
    @SuppressWarnings("unchecked")
	public final static <U> U getSessionVisitor(){
    	return (U)getContext(VISITOR_KEY);
    }
    
    /**
     * 设置做分表分库的切分的key
     * @param shardKey
     */
    public final static <K extends Serializable, U> void putSessionVisitor(U sessionVisitor){
    	putContext(VISITOR_KEY, sessionVisitor);
    }
    
    /**
     * 获取Session中的用户信息
     * @return
     */
    @SuppressWarnings("unchecked")
	public final static <U> U getSessionUser(){
    	return (U)getContext(USER_KEY);
    }
    
    /**
     * 设置做分表分库的切分的key
     * @param shardKey
     */
    public final static <K extends Serializable, U> void putSessionUser(U exUser){
    	putContext(USER_KEY, exUser);
    }
    /**
     * 设置api 请求头信息
     * @param apiHeaderVo
     */
    public final static <K> void putApiHeader(K apiHeaderVo){
    	putContext(API_HEADER_KEY, apiHeaderVo);
    }
    
    /**
     * 获取api 请求头信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public final static <K> K getApiHeader(){
    	K apiHeaderVo=(K)getContext(API_HEADER_KEY);
    	if(apiHeaderVo==null) {
    		apiHeaderVo=(K) new ApiHeaderVo();
    	}
    	return apiHeaderVo;
    }
    
    
    /**
     * 线程日志的级别
     * @param logLevel
     */
    public final static void putThreadLog(Integer logLevel){
    	putContext(THREAD_LOG_KEY, logLevel);
    }
    
    /**
     * 获取线程日志的级别
     * @return
     */
    public final static Integer getThreadLog(){
    	return getContext(THREAD_LOG_KEY);
    }
    
    public final static void putUserAuth(List<String> permsList) {
    	putContext(USER_AUTH_KEY, permsList);
    }
    public final static List<String> getUserAuth(){
    	return getContext(USER_AUTH_KEY);
    }
    private final static String API_HEADER_KEY = "_api_header_key";
    /**
     * 用来做分库分表的切分ID
     */
    private final static String SHARD_KEY = "shardKey";
    
    /**
     * 当前Session中登陆的user
     */
    private final static String VISITOR_KEY = "sessionVisitor";
    
    
    /**
     * 当前Session中登陆的user
     */
    private final static String USER_KEY = "sessionUser";
    
    
    private final static String USER_AUTH_KEY = "sessionUserAuth";
    
    /**
     * 线程的日志级别
     */
    private final static String THREAD_LOG_KEY = "threadLog";
}
